<?php
class omekpi_ctl_admin_pickinger extends desktop_controller{
    var $name = "配货绩效";
    var $workground = "performance";

    function index(){
        $pickinger_log = &$this->app->model('pickinger_log');
        $pickList = $pickinger_log->getPageData();
        $delivery = &app::get('ome')->model('delivery');
        $res = $delivery->getList('*',array('parent_id'=>'0','type'=>'normal','status'=>array('succ','failed','progress','timeout','ready','stop')));
        $countres = count($res);
        $count = $pickinger_log->getCount();
        $this->pagedata['count'] = $count;
        $this->pagedata['begin_date'] = $_POST['begin_date'];
        $this->pagedata['end_date'] = $_POST['end_date'];
        $this->pagedata['pickList'] = $pickList;
        $this->pagedata['countres'] = $countres;
        $this->pagedata['csv_data'] = serialize($pickList);
        $this->page("admin/performance/pickinger_log.html");
    }

    
    function export(){
        $this->begin("index.php#app=omekpi&ctl=admin_pickinger&act=index");
        $res = unserialize(stripcslashes($_POST['csv_data']));
        $mess = '员工编号,员工姓名,需配货共计,已配货共计,我的配货数量,日期';
        if($res){
            foreach($res as $value){
                $mess .= "\r".$value['employee_op_no'].",".
                         $value['employee_name'].",".
                         $value['pick_numbers'].",".
                         $value['picked_numbers'].",".
                         $value['my_picked_numbers'].",".
                         $value['createdate']."\r";
            }
        }
        header("Content-type: application/octet-stream");
        header("Accept-Ranges:bytes");
        $filename = '配货绩效'.date('m月d日',time()).".csv";
        $encoded_filename = urlencode($filename);
        $encoded_filename = str_replace("+", "%20", $encoded_filename);
        
        $ua = $_SERVER["HTTP_USER_AGENT"];
        if (preg_match("/MSIE/", $ua)) {
            header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
        } else if (preg_match("/Firefox/", $ua)) {
            header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');
        } else {
            header('Content-Disposition: attachment; filename="' . $filename . '"');
        }
        //header("Content-Disposition: attachment; filename=".'配货绩效'.date('m月d日',time()).".csv");
        echo mb_convert_encoding($mess,"GBK","UTF-8");
    }
    
    function exportTmpl(){
        $mess='';
        $mess .= '员工帐号1,快递单号a,快递单号e,快递单号i,快递单号1'."\r".
                 '员工帐号2,快递单号b,快递单号f,快递单号3,快递单号t'."\r".
                 '员工帐号3,快递单号c,快递单号g,快递单号5,快递单号y'."\r".
                 '员工帐号4,快递单号d,快递单号h,快递单号6,快递单号0'."\r".
                 '请用记事本对下载的csv文件进行编辑，Execl可能导致过长的物流单号在导入时不正确，并确保每条数据末尾没有逗号'."\r";
        header("Content-type: application/octet-stream");
        header("Accept-Ranges:bytes");
        $filename = '配货绩效模板'.date('m月d日',time()).".csv";
        $encoded_filename = urlencode($filename);
        $encoded_filename = str_replace("+", "%20", $encoded_filename);
        
        $ua = $_SERVER["HTTP_USER_AGENT"];
        if (preg_match("/MSIE/", $ua)) {
            header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
        } else if (preg_match("/Firefox/", $ua)) {
            header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');
        } else {
            header('Content-Disposition: attachment; filename="' . $filename . '"');
        }
        //header("Content-Disposition: attachment; filename=".'配货绩效模板'.date('m月d日',time()).".csv");
        echo mb_convert_encoding($mess,"GBK","UTF-8");
    }

    function importTable(){
        //$this->begin("index.php#app=omekpi&ctl=admin_pickinger&act=index");
        $file = $_FILES['package'];
        $extname = strtolower($this->extName($file['name']));
        
        $pickinger_invoice = &$this->app->model('pickinger_invoice');
        $pickinger_log = &$this->app->model('pickinger_log');
        $delivery = app::get('ome')->model('delivery');
        $employee = &$this->app->model('employee');
        $dcount = $delivery->count();
        if($extname=='.csv'){
            $content = file_get_contents($file['tmp_name']);
            $array = explode("\r",$content);//按行
            if($array[count($array)-1]=="\r"){
                array_pop($array);
            }
            $i=0;
            $employee_ids = array();
            if($array){
                for($i=0;$i<count($array);$i++){
                    $ar = explode(',',$array[$i]);
                    $employee_login_name=$ar[0];
                    $employee_login_name=str_replace("\r","",str_replace("\n","",$employee_login_name));
                    $employee_login_name = kernel::service('ectools_charset')->local2utf($employee_login_name);
                    $em_filter = array('employee_login_name'=>$employee_login_name,'employee_type'=>'pickinger');
                    $em = $employee->dump($em_filter);
                    if(!$em){
                        echo "<script>parent.MessageBox.error(".$employee_login_name.'不在配货员列表中'.");</script>";
//                        $this->end(false,$ar[0].'不在打包员列表中');
                        return false;
                    }
                    
                    for($k=1;$k<count($ar);$k++){
                        $del_filter = array('logi_no'=>$ar[$k]);
                        $del = $delivery->dump($del_filter,'*');
                        $del2 = $pickinger_invoice->dump($del_filter,'logi_no');
                        if(!$del){
                            echo "<script>parent.MessageBox.error('该快递单号不存在');</script>";
                            return false;
//                            $this->end(false,'该快递单号不存在');
                        }
                        if($del2){
                            echo "<script>parent.MessageBox.error('不能重复导入已导入过的单号');</script>";
                            return false;
                        }
                        $del_filter=null;$del=null;
                    }
                    array_push($employee_ids,$em['employee_id']);
                    $ar = null;$em_filter=null;$em=null;
                }
                $allcount=$pickinger_log->getContentPickedCount();//查询得出
                if(!$allcount) $allcount =0;
                for($e=0;$e<count($array);$e++){
                    $arr = explode(',',$array[$e]);
                    $allcount += count($arr)-1;
                    for($j=1;$j<count($arr);$j++){
                        $invoice_data['employee_id'] = $employee_ids[$e];
                        $invoice_data['logi_no'] = $arr[$j];
                        $invoice_data['createdate'] = date('Ymd');
                       
                        $pickinger_invoice->save($invoice_data);
                        
                        $invoice_data=null;
                    }
                    
                    $time = date('Ymd');
                    $pkg_filter = array('employee_id'=>$employee_ids[$e],'createdate'=>$time);
                    $res = $pickinger_log->dump($pkg_filter);
                    if($res){
                        $pick_data['employee_id'] = $employee_ids[$e];
                        $pick_data['createdate'] = $time;
                        $pick_data['my_picked_numbers'] = $res['my_picked_numbers']+count($arr)-1;
                        $pickinger_log->update_mypicked($pick_data);
                        $pick_data = null;
                    }else{
                        $pick_array['employee_id'] = $employee_ids[$e];
                        $pick_array['pick_numbers']=1000;
                        $pick_array['picked_numbers']=1;
                        $pick_array['my_picked_numbers'] = count($arr)-1;
                        $pick_array['createdate']=$time;
                        $pickinger_log->save($pick_array);
                        $pick_array=null;
                    }
                    $arr=null;
                }
            $pickinger_log->updateCount($dcount,$allcount);
            echo "<script>parent.MessageBox.success('上传完成');</script>";
//            $this->end(true,'上传完成');
            }
        }else{
            echo "<script>parent.MessageBox.error('必须是CSV文件');</script>";
//            $this->end(false,'必须是csv文件');
        }
    }
    


    function extName($file){
        return substr($file,strrpos($file,'.'));
    }
    
    function getPagedata($sList=array(),$slog_List=array()){
        $omekpi=array();
        foreach($sList as $ke=>$va){//人员列表
            foreach($slog_List as $k=>$v){//绩效列表
                if($v['employee_id'] == $va['employee_id']){ 
                    $array['employee_id'] =$v['employee_id'];
                    $array['employee_name'] = $va['employee_name'];
                    $array['employee_op_no'] = $va['employee_op_no'];
                    $array['pick_numbers'] = $v['pick_numbers'];
                    $array['picked_numbers'] = $v['picked_numbers'];
                    $array['my_picked_numbers'] = $v['my_picked_numbers'];
                    $array['createdate'] = $v['createdate'];
                    array_push($omekpi,$array);
                    $array = null;
                }
            }
        }
        return $omekpi;
    }
    
    function search(){
        $this->begin('index.php?app=omekpi&ctl=admin_pickinger&act=index');
        $pickinger_log = &$this->app->model('pickinger_log');
        $employee= $this->app->model('employee');
        if (!$_POST['begin_date']&&!$_POST['end_date']&&!$_POST['have']){
            $this->end(true,'搜索完成');
        }

        $begin_date = date('Ymd',strtotime($_POST['begin_date']));
        $end_date = date('Ymd',strtotime($_POST['end_date']));
        if($_POST['begin_date'] && $_POST['end_date'] && ($begin_date > $end_date)){
            $this->end(false,'开始时间大于结束时间');
        }

        if (!$_POST['have']){//如果没有名字
            $where = 'where 1 ';
            if($_POST['begin_date']){
            $where .= "and createdate >= '".$begin_date."'";
            }
            if($_POST['end_date']){
                $where .= "and createdate <= '".$end_date."'";
            }
            $slogList = $pickinger_log->search($where);
            $em_filter = array('employee_type'=>'pickinger'); 
            $sList = $employee->getList('*',$em_filter,0,-1);
            $searchList = $this->getPagedata($sList,$slogList);
            if(!$searchList){
                $this->end(true,'无当前条件结果');
            }
            $count = $pickinger_log->getCount($where);
        }else{
            $filter=array();
            if($_POST['select']=='NA' ){
                $filter['employee_name'] = $_POST['have'];
            }else{
                if($_POST['select']=='NO' ){
                    $filter['employee_op_no'] = $_POST['have'];
                }
            }
            $filter['employee_type'] = 'pickinger';
            $sList = $employee->getList('*',$filter,0,-1);
            $where = ' where 1 ';
            $in = '';
            if($sList){
                foreach($sList as $v){
                    $in .=$v['employee_id'].',';
                }
                $where .=' and employee_id in ('.substr($in,0,strlen($in)-1).')';
            }else{
                $this->end(false,$_POST['have'].'不在配货员列表中');
            }
            if($_POST['begin_date']){
                $where .= " and createdate >= '".$begin_date."'";
            }
            
            if($_POST['end_date']){
                $where .= " and createdate <= '".$end_date."'";
            }
            $slogList = $pickinger_log->search($where);
            $searchList = $this->getPagedata($sList,$slogList);
            if(!$searchList){
                $this->end(true,'无当前条件结果');
            }
            $count = $pickinger_log->getCount($where);
        }
        $this->pagedata['count']=$count;
        $this->pagedate['employee_op_no']=$_POST['employee_op_no'];
        $this->pagedata['employee_name']=$_POST['employee_name'];
        $this->pagedata['begin_date'] = $_POST['begin_date'];
        $this->pagedata['end_date'] = $_POST['end_date'];
        $this->pagedata['pickList'] = $searchList;
        $this->pagedata['csv_data'] = serialize($searchList);
        $this->page('admin/performance/pickinger_log.html');
    }
    
    
    
    function getPicking(){
//          $filter['process']='FALSE';
        $filter['parent_id'] = 0;
        $this->finder('ome_mdl_delivery',array(
            'title' => '需配货共计管理',
            'base_filter' => $filter,
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag'=>false,
            'use_buildin_recycle'=>false,
            'use_buildin_export'=>false,
            'use_buildin_import'=>false,
            'use_buildin_filter'=>true,
       ));
    }

    function getPickinged(){
        $pickinger_invoice = $this->app->model('pickinger_invoice');
        $delivery = app::get('ome')->model('delivery');
        $logi_no_ids = $pickinger_invoice->getList('logi_no','',0,-1);
        
        $d_ids=array();

        if($logi_no_ids){
            foreach($logi_no_ids as $v){
                $d_id = $delivery->dump(array('logi_no'=>$v['logi_no']),'delivery_id');
                array_push($d_ids,$d_id['delivery_id']);
            }
        }
        $filter['delivery_id'] = $d_ids;
        $filter['parent_id'] = 0;
        $this->finder('ome_mdl_delivery',array(
            'title' => '已配货共计管理',
            'base_filter' => $filter,
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag'=>false,
            'use_buildin_recycle'=>false,
            'use_buildin_export'=>false,
            'use_buildin_import'=>false,
            'use_buildin_filter'=>true,
       ));
    }

    function getMypickinged(){
        $employee_id = $_GET['id'];
        $createdate=date('Ymd',strtotime($_GET['date']));
        $delivery = app::get('ome')->model('delivery');
        $pickinger_invoice = $this->app->model('pickinger_invoice');
        $logi_no_ids =  $pickinger_invoice->getList('logi_no',array('employee_id'=>$employee_id),0,-1);
        $d_ids=array();
        if($logi_no_ids){
            foreach($logi_no_ids as $v){
                $d_id = $delivery->dump(array('logi_no'=>$v['logi_no']),'delivery_id');
                array_push($d_ids,$d_id['delivery_id']);
            }
        }
        $filter['delivery_id'] = $d_ids;
        $this->finder('ome_mdl_delivery',array(
            'title' => '我的配货单管理',
            'base_filter' => $filter,
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag'=>false,
            'use_buildin_recycle'=>false,
            'use_buildin_export'=>false,
            'use_buildin_import'=>false,
            'use_buildin_filter'=>true,
       ));

    }

}
?>
